package com.paradox.aggregation.consumer.controller;

import com.paradox.aggregation.consumer.entity.Providers;
import com.paradox.aggregation.consumer.mapper.ProvidersMapper;
import com.paradox.aggregation.consumer.service.ConsumerService;
import com.paradox.aggregation.providerAPI.api.DubboTestService;
import com.paradox.aggregation.providerAPI.api.ProviderService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private ProviderService providerService;
    @Autowired
    private ProvidersMapper providersMapper;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private RestTemplate restTemplate;
    //@Reference(version = "default")
    //private DubboTestService dubboTestService;

    @DubboReference(version = "async")
    private DubboTestService asyncDubboTestService;

    @GetMapping("/getSource")
    public String getSource(@RequestHeader HttpHeaders headers){
        System.out.println(headers.getFirst("tokenInfo"));
        String res=restTemplate.getForObject("http://provider/provider/getSource",String.class);
        //return res;
        return providerService.getSource();
    }

    @GetMapping("/getProviders")
    public Object getProviders(){
        consumerService.getProviders();
        return "null";
    }

    /*@GetMapping("/getSource2")
    public String getSource2(){
        return dubboTestService.getSource("hello world").toString();
    }*/

    /*@GetMapping("/getSource3")
    public String getSource3() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future= asyncDubboTestService.getSource("hello world");
        future.whenComplete((s,e) -> {
            System.out.println("xxxxxx");
        });
        return future.get();
    }*/
}
